package azcat.planner.gui;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.MenuKeyListener;
import javax.swing.event.MenuKeyEvent;
import javax.swing.filechooser.FileFilter;
import javax.swing.JInternalFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.ComponentOrientation;
import java.awt.Component;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;

import azcat.planner.materials.Material;
import azcat.planner.materials.MaterialsManager;
import azcat.planner.materials.NeededMaterialsEstimator;
import azcat.planner.schedule.Schedule;
import azcat.planner.tools.Tool;
import azcat.planner.workers.Worker;
import azcat.planner.workers.WorkforceManager;

import java.awt.SystemColor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.nio.file.OpenOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.ScrollPaneConstants;

public class MainWindow extends JFrame{	
	
	private	JLabel label_1;
	private	JLabel label_2;
	private	JLabel label_3;
	private	JLabel label_4;
	private	JLabel label_5;
	private	JLabel label_6;
	private	JLabel label_7;
	private	JLabel label_8;
	private	JLabel label_9;
	private	JLabel label_10;
	private	JLabel label_11;
	private	JLabel label_12;


	private JFrame frame;
	private MaterialsType matType;
	private ToolsWindow toolsWindow;
	private JPanel toolsTitle;
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenuItem mntmNew;
	private JMenuItem mntmOpenProject;
	private JSeparator separator;
	private JMenuItem mntmSave;
	private JMenuItem mntmExit;
	private JPanel panel;
	private JPanel leftPanel;
	private JPanel materialsPanel;
	private JPanel materialTitle;
	private JLabel lblMateriales;
	private JPanel materialsInfo;
	private JLabel lblType;
	private JLabel lblQty;
	private JLabel lblType_1;
	private JLabel lblQty_1;
	private JLabel lblPolietileno;
	private JLabel lblPoliValue;
	private JLabel lblBurbuja;
	private JLabel lblBurbujaValue;
	private JLabel lblRpvc;
	private JLabel lblRpvcValue;
	private JLabel lblCarton;
	private JLabel lblCartonValue;
	private JLabel lblCrosslink;
	private JLabel lblCrosslinkValue;
	private JLabel lblFoam;
	private JLabel lblFoamValue;
	private JLabel lblAbs;
	private JLabel lblAbsValue;
	private JLabel lblPlastico;
	private JLabel lblPlasticoValue;
	private JPanel panel_2;
	private JPanel materialsButton;
	private JButton btnEstimar;
	private JPanel panel_3;
	private JPanel toolsPanel;
	private JPanel panel_1;
	private JLabel lblHerramientas;
	private JPanel toolsInfo;
	private JPanel toolsButton;
	private JButton btnSelecHerra;
	private JPanel rightPanel;
	private JPanel workersPanel;
	private JPanel workersTitle;
	private JLabel lblTrabajadores;
	private JPanel workersInfo;
	private JPanel workersButton;
	protected ArrayList<JLabel> toolsLabels = new ArrayList<JLabel>();
	private NeededMaterials nm;
	private ArrayList<Material> neededMaterials;
	private ToolsData toolsdata;
	private ArrayList<Tool> neededTools;
	private ArrayList<Worker> neededWorkers;
	private JButton btnDetalles;
	private WorkforceManager wfm;
	private JPanel projectPanel;
	private JPanel projectTitle;
	private JLabel lblNewLabel;
	private JPanel projectInfo;
	private JPanel projectButton;
	private JButton btnNewButton;	
	private final JFileChooser fc = new JFileChooser();
	private int userSelection;
	private ArrayList<String> toolDates = new ArrayList<String>();
	private ArrayList<String> workersDates = new ArrayList<String>();
	private JLabel lblNumeroDeParte;
	private JLabel lblFechaDeEntrega;
	private JLabel lblFechaEstimadaDe;
	private JLabel lblNoTrabajadoresNecesarios;
	private JLabel lblneededWorkers;
	private JLabel lblEstimatedTime;
	private int count;
	private String estimatedDelivery;
	private JLabel lblPartNumber;
	private JLabel lblDeadline;
	private NuevoProyecto nuevo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					MainWindow window = new MainWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	protected void addLabels(){

	}	
	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
		createFrames();
		addLabels();
	}
	private void createFrames(){
		toolsWindow = new ToolsWindow(this);
		matType = new MaterialsType();
	}
	
	protected void displaySummaryOfMats(){
		MaterialsManager manager = nm.getManager();
		//neededMaterials = manager.getNeededMaterialsList();
		int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h =0;
		for(int i =0; i < neededMaterials.size(); i++){
			if(neededMaterials.get(i).getMaterialType().equals("Placas de Polietileno")){
				a+=neededMaterials.get(i).getAmountNeeded();
			}
			else if(neededMaterials.get(i).getMaterialType().equals("RPVC")){
				b+=neededMaterials.get(i).getAmountNeeded();
			}
			else if(neededMaterials.get(i).getMaterialType().equals("Crosslink")){
				c+=neededMaterials.get(i).getAmountNeeded();
			}
			else if(neededMaterials.get(i).getMaterialType().equals("ABS")){
				d+=neededMaterials.get(i).getAmountNeeded();
			}
			else if(neededMaterials.get(i).getMaterialType().equals("Burbuja")){
				e+=neededMaterials.get(i).getAmountNeeded();
			}
			else if(neededMaterials.get(i).getMaterialType().equals("Carton")){
				f+=neededMaterials.get(i).getAmountNeeded();
			}
			else if(neededMaterials.get(i).getMaterialType().equals("Rollos de Foam")){
				g+=neededMaterials.get(i).getAmountNeeded();
			}
			else if(neededMaterials.get(i).getMaterialType().equals("Plastico Corrugado")){
				h+=neededMaterials.get(i).getAmountNeeded();
			}
		}
		lblPoliValue.setText(a+"");
		lblRpvcValue.setText(b+"");
		lblCrosslinkValue.setText(c+"");
		lblAbsValue.setText(d+"");
		lblBurbujaValue.setText(e+"");
		lblCartonValue.setText(f+"");
		lblFoamValue.setText(g+"");
		lblPlasticoValue.setText(h+"");
		
		btnSelecHerra.setEnabled(true);
		btnEstimar.setEnabled(true);
	}
	private void createOpenDialog(){
		userSelection = fc.showOpenDialog(this);
	}
	private void createSaveDialog(){
		userSelection = fc.showSaveDialog(this);
	}
	private void createNuevoProyecto(){
		nuevo = new NuevoProyecto(this);
	}
	public void enableMaterialbutton(){
		btnEstimar.setEnabled(true);
	}
	public void disableMaterialButton(){
		btnEstimar.setEnabled(false);
	}
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		
		frame = new JFrame();
		frame.setBounds(100, 100, 1090, 830);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		mntmNew = new JMenuItem("New");
		mntmNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createNuevoProyecto();
				nuevo.setVisible(true);
				
			}
		});
		mnFile.add(mntmNew);
		
		mntmOpenProject = new JMenuItem("Open Project");
		mntmOpenProject.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createOpenDialog();
				File fileToOpen =null;
				if (userSelection == JFileChooser.APPROVE_OPTION) {
					fileToOpen = fc.getSelectedFile();
					createNm();
					nm.openEstimations(fileToOpen);
					nm.setVisible(true);
					readToolsSchedule(fileToOpen);
				}				
			}
		});
		mnFile.add(mntmOpenProject);
		
		separator = new JSeparator();
		mnFile.add(separator);
		
		mntmSave = new JMenuItem("Save");
		mntmSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//writeobjects();
				createSaveDialog();
				File fileToSave =null;				
				if (userSelection == JFileChooser.APPROVE_OPTION ) {
					if(nm == null){
						createNm();
						fileToSave = fc.getSelectedFile();
						nm.saveEstimations(fileToSave);
						saveEstimations(fileToSave);
					}
					else{
						fileToSave = fc.getSelectedFile();
						nm.saveEstimations(fileToSave);
						saveEstimations(fileToSave);
					}
				}
				
			}
		});
		mnFile.add(mntmSave);
		
		JSeparator separator_1 = new JSeparator();
		mnFile.add(separator_1);
		
		mntmExit = new JMenuItem("Exit");		
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
		
		mnFile.add(mntmExit);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		leftPanel = new JPanel();
		leftPanel.setBorder(null);
		leftPanel.setPreferredSize(new Dimension(530, 765));
		panel.add(leftPanel);
		leftPanel.setLayout(new BorderLayout(0, 0));
		
		materialsPanel = new JPanel();
		materialsPanel.setBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(0, 0, 0)));
		leftPanel.add(materialsPanel, BorderLayout.NORTH);
		materialsPanel.setLayout(new BorderLayout(0, 0));
		
		materialTitle = new JPanel();
		materialTitle.setBorder(new MatteBorder(0, 1, 0, 1, (Color) new Color(0, 0, 0)));
		materialsPanel.add(materialTitle, BorderLayout.NORTH);
		
		lblMateriales = new JLabel("Materiales");
		lblMateriales.setFont(new Font("Tahoma", Font.BOLD, 18));
		materialTitle.add(lblMateriales);
		
		materialsInfo = new JPanel();
		materialsInfo.setBorder(new MatteBorder(0, 1, 0, 1, (Color) new Color(0, 0, 0)));
		materialsPanel.add(materialsInfo, BorderLayout.CENTER);
		materialsInfo.setLayout(new MigLayout("", "[][125.00px][65.00px][53.00][94.00px][125.00px][65.00px][54.00][]", "[35.00][30.00px][30.00px][30.00px][30.00px]"));
		
		lblType = new JLabel("Tipo");
		lblType.setFont(new Font("Tahoma", Font.BOLD, 13));
		materialsInfo.add(lblType, "cell 1 0");
		
		lblQty = new JLabel("# Blocks o Placas");
		lblQty.setFont(new Font("Tahoma", Font.BOLD, 13));
		materialsInfo.add(lblQty, "cell 2 0 2 1");
		
		lblType_1 = new JLabel("Tipo");
		lblType_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		materialsInfo.add(lblType_1, "cell 5 0");
		
		lblQty_1 = new JLabel("# Blocks o Placas");
		lblQty_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		materialsInfo.add(lblQty_1, "cell 6 0 2 1");
		
		lblPolietileno = new JLabel("Polietileno");
		materialsInfo.add(lblPolietileno, "cell 1 1,alignx left,aligny center");
		
		lblPoliValue = new JLabel("0");
		materialsInfo.add(lblPoliValue, "cell 2 1,alignx center");
		
		lblBurbuja = new JLabel("Burbuja");
		materialsInfo.add(lblBurbuja, "cell 5 1,alignx left,aligny center");
		
		lblBurbujaValue = new JLabel("0");
		materialsInfo.add(lblBurbujaValue, "cell 6 1,alignx center");
		
		lblRpvc = new JLabel("RPVC");
		materialsInfo.add(lblRpvc, "cell 1 2,alignx left,aligny center");
		
		lblRpvcValue = new JLabel("0");
		materialsInfo.add(lblRpvcValue, "cell 2 2,alignx center");
		
		lblCarton = new JLabel("Carton");
		materialsInfo.add(lblCarton, "cell 5 2");
		
		lblCartonValue = new JLabel("0");
		materialsInfo.add(lblCartonValue, "cell 6 2,alignx center");
		
		lblCrosslink = new JLabel("Crosslink");
		materialsInfo.add(lblCrosslink, "cell 1 3,alignx left,aligny center");
		
		lblCrosslinkValue = new JLabel("0");
		materialsInfo.add(lblCrosslinkValue, "cell 2 3,alignx center");
		
		lblFoam = new JLabel("Foam");
		materialsInfo.add(lblFoam, "cell 5 3");
		
		lblFoamValue = new JLabel("0");
		materialsInfo.add(lblFoamValue, "cell 6 3,alignx center");
		
		lblAbs = new JLabel("ABS");
		materialsInfo.add(lblAbs, "cell 1 4,alignx left,aligny center");
		
		lblAbsValue = new JLabel("0");
		materialsInfo.add(lblAbsValue, "cell 2 4,alignx center");
		
		lblPlastico = new JLabel("Plastico");
		materialsInfo.add(lblPlastico, "cell 5 4");
		
		lblPlasticoValue = new JLabel("0");
		materialsInfo.add(lblPlasticoValue, "cell 6 4,alignx center");
		
		panel_2 = new JPanel();
		materialsPanel.add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		materialsButton = new JPanel();
		materialsButton.setBorder(new MatteBorder(0, 1, 1, 1, (Color) new Color(0, 0, 0)));
		panel_2.add(materialsButton, BorderLayout.NORTH);
		materialsButton.setLayout(new MigLayout("", "[9.00][71px][][]", "[23px]"));
		
		btnEstimar = new JButton("Estimar");
		btnEstimar.setEnabled(false);
		btnEstimar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(nm == null){
					btnSelecHerra.setEnabled(false);
					btnDetalles.setEnabled(false);
					//System.out.println("Is null");
					createNm();
					nm.setVisible(true);
				}
				btnSelecHerra.setEnabled(false);
				btnDetalles.setEnabled(false);
				nm.setVisible(true);
			}
		});
		materialsButton.add(btnEstimar, "cell 1 0");
		
		panel_3 = new JPanel();
		panel_2.add(panel_3, BorderLayout.SOUTH);
		
		toolsPanel = new JPanel();
		toolsPanel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		leftPanel.add(toolsPanel, BorderLayout.CENTER);
		toolsPanel.setLayout(new BorderLayout(0, 0));
		
		toolsTitle = new JPanel();
		toolsPanel.add(toolsTitle, BorderLayout.NORTH);
		toolsTitle.setLayout(new BorderLayout(0, 0));
		
		panel_1 = new JPanel();
		panel_1.setBorder(null);
		toolsTitle.add(panel_1, BorderLayout.SOUTH);
		
		lblHerramientas = new JLabel("Herramientas");
		panel_1.add(lblHerramientas);
		lblHerramientas.setAlignmentX(Component.CENTER_ALIGNMENT);
		lblHerramientas.setFont(new Font("Tahoma", Font.BOLD, 18));
		
		toolsInfo = new JPanel();
		JScrollPane scroller = new JScrollPane(toolsInfo);
		scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		toolsPanel.add(scroller, BorderLayout.CENTER);
		toolsInfo.setLayout(new MigLayout("", "[517.00]", "[22.00][][][][][][][][][][][]"));
		
		toolsButton = new JPanel();
		toolsPanel.add(toolsButton, BorderLayout.SOUTH);
		toolsButton.setLayout(new MigLayout("", "[9.00][71px][99px][67px]", "[23px]"));
		
		btnSelecHerra = new JButton("Selec. Hmtas.");
		btnSelecHerra.setEnabled(false);
		btnSelecHerra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {		
				if(toolsdata == null){
					btnDetalles.setEnabled(false);
					createToolsData();
					toolsdata.setVisible(true);					
				}
				else{
					btnDetalles.setEnabled(false);
					showToolsData();
					toolsdata.setVisible(true);
				}
				//toolsWindow.setVisible(true);
			}
		});
		toolsButton.add(btnSelecHerra, "cell 1 0,alignx left,aligny top");
		
		rightPanel = new JPanel();
		rightPanel.setPreferredSize(new Dimension(530, 765));
		panel.add(rightPanel);
		rightPanel.setLayout(new BorderLayout(0, 0));
		
		workersPanel = new JPanel();
		workersPanel.setPreferredSize(new Dimension(10, 350));
		workersPanel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		rightPanel.add(workersPanel, BorderLayout.NORTH);
		workersPanel.setLayout(new BorderLayout(0, 0));
		
		workersTitle = new JPanel();
		workersPanel.add(workersTitle, BorderLayout.NORTH);
		
		lblTrabajadores = new JLabel("Trabajadores");
		lblTrabajadores.setFont(new Font("Tahoma", Font.BOLD, 18));
		workersTitle.add(lblTrabajadores);
		
		workersButton = new JPanel();
		workersPanel.add(workersButton, BorderLayout.SOUTH);
		workersButton.setLayout(new MigLayout("", "[17.00][71px]", "[23px]"));
		
		btnDetalles = new JButton("Estimar");
		btnDetalles.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createWorkforceManager();
				neededWorkers = wfm.estimateWorkforce();
				//System.out.println("size: "+neededWorkers.size());
				displayWorkersDates();
				btnNewButton.setEnabled(true);
				
			}
		});
		btnDetalles.setEnabled(false);
		workersButton.add(btnDetalles, "cell 1 0,alignx left,aligny top");
		
		workersInfo = new JPanel();
		JScrollPane scroller2 = new JScrollPane(workersInfo);
		scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);		
		workersPanel.add(scroller2, BorderLayout.CENTER);
		workersInfo.setLayout(new MigLayout("", "[517.00]", "[22.00][][][][][][][][][]"));
		
		projectPanel = new JPanel();
		projectPanel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		rightPanel.add(projectPanel, BorderLayout.CENTER);
		projectPanel.setLayout(new BorderLayout(0, 0));
		
		projectTitle = new JPanel();
		projectTitle.setBorder(new MatteBorder(0, 0, 1, 0, (Color) new Color(0, 0, 0)));
		projectPanel.add(projectTitle, BorderLayout.NORTH);
		
		lblNewLabel = new JLabel("Proyecto");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		projectTitle.add(lblNewLabel);
		
		projectInfo = new JPanel();
		projectPanel.add(projectInfo, BorderLayout.CENTER);
		projectInfo.setLayout(new MigLayout("", "[][][][]", "[][28.00][][28.00][][28.00][]"));
		
		lblNumeroDeParte = new JLabel("Numero de Parte:");
		lblNumeroDeParte.setFont(new Font("Tahoma", Font.BOLD, 13));
		projectInfo.add(lblNumeroDeParte, "cell 0 0");
		
		lblPartNumber = new JLabel("");
		projectInfo.add(lblPartNumber, "cell 3 0,alignx left");
		
		lblFechaDeEntrega = new JLabel("Fecha de Entrega:");
		lblFechaDeEntrega.setFont(new Font("Tahoma", Font.BOLD, 13));
		projectInfo.add(lblFechaDeEntrega, "cell 0 2");
		
		lblDeadline = new JLabel("");
		projectInfo.add(lblDeadline, "cell 3 2");
		
		lblFechaEstimadaDe = new JLabel("Fecha Estimada de Entrega:");
		lblFechaEstimadaDe.setFont(new Font("Tahoma", Font.BOLD, 13));
		projectInfo.add(lblFechaEstimadaDe, "cell 0 4");
		
		lblEstimatedTime = new JLabel("");
		projectInfo.add(lblEstimatedTime, "cell 3 4,alignx left");
		
		lblNoTrabajadoresNecesarios = new JLabel("No. Trabajadores Necesarios: ");
		lblNoTrabajadoresNecesarios.setFont(new Font("Tahoma", Font.BOLD, 13));
		projectInfo.add(lblNoTrabajadoresNecesarios, "cell 0 6");
		
		lblneededWorkers = new JLabel("");
		projectInfo.add(lblneededWorkers, "cell 3 6,alignx left");
		
		projectButton = new JPanel();
		FlowLayout flowLayout = (FlowLayout) projectButton.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		projectPanel.add(projectButton, BorderLayout.SOUTH);
		
		btnNewButton = new JButton("Estimar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setProjectDisplays();
			}
		});
		btnNewButton.setEnabled(false);
		btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		projectButton.add(btnNewButton);
	}
	private void createWorkforceManager(){
		wfm = new WorkforceManager(this.neededTools);
	}
	private void createToolsData(){
		toolsdata = new ToolsData(neededMaterials, this);
	}
	private void showToolsData(){
		toolsdata.repaintGUI(neededMaterials, this);
	}
	private void createNm(){
		nm = new NeededMaterials(this);
	}
	public void setNeededTools(ArrayList<Tool> list){
		this.neededTools = list;
	}
	public void setNeededMaterials(ArrayList<Material> mats){
		this.neededMaterials = mats;
	}
	public void enableWorkersButton(){
		btnDetalles.setEnabled(true);
	}
	public void enableMaterialsButton(){
		btnEstimar.setEnabled(true);
	}
	public void displayWorkersDates(){
		workersInfo.removeAll();
		workersInfo.updateUI();
		workersPanel.updateUI();
		JLabel label = new JLabel();
		int count = 0;
		for(int i=0; i< neededWorkers.size(); i++){
			//System.out.println(neededWorkers.get(i).getfName() +" " +neededWorkers.get(i).getlName());			
			ArrayList<Schedule> s = neededWorkers.get(i).getSchedule();
			//System.out.println("Schedule: " +s.size());
			for(int j =0; j<s.size(); j++){
				label = new JLabel();
				label.setFont(new Font("Tahoma", Font.PLAIN, 13));
				label.setAlignmentX(Component.CENTER_ALIGNMENT);
				String txt =neededWorkers.get(i).getfName() + " "+ neededWorkers.get(i).getlName() + " usando "+neededWorkers.get(i).getWorkingOnTool(j)+", en " + s.get(j).printWorkerDate();
				workersDates.add(txt);
				label.setText(txt);
				Object o = "cell 0 " + count + ",alignx left";
				workersInfo.add(label, o);
				count++;
				//System.out.println(s.get(j).printDate());
			}
		}
	}
	/**
	 * Save the estimations for tools and workers
	 * @param f
	 */
	public void saveEstimations(File f){
		ArrayList<String> estimations = toolDates;
		ArrayList<String> estimations2 = workersDates;
		if(estimations != null){
			try {
				PrintWriter writer = new PrintWriter(new FileWriter(f,true));
				for(int i=0; i<estimations.size(); i++ ){
					writer.println("tool"+","+ estimations.get(i));					
				}
				for(int i=0; i<estimations2.size(); i++ ){
					writer.println("worker"+","+ estimations2.get(i));					
				}
				writer.println("Needed Workers,"+ count);
				writer.println("Estimated Time," + estimatedDelivery);
				writer.println("PartNo," + lblPartNumber.getText());
				writer.println("Deadline," + lblDeadline.getText());		
				writer.close();
				
			} catch (Exception e) {}
		}
	}
	public void clearAll(){
		toolsInfo.removeAll();
		toolsInfo.updateUI();
		workersInfo.removeAll();
		workersInfo.updateUI();
		workersPanel.updateUI();		 
		lblneededWorkers.setText("");
		lblEstimatedTime.setText("");
		//lblPartNumber.setText("");
		//lblDeadline.setText("");
		createNm();
		//createToolsData();
		//createWorkforceManager();
         
	}
	/**
	 * displays the estimations made for the tools
	 */
	public void displayDates(){
		//Component [] labels = toolsInfo.getComponents();
		toolsInfo.removeAll();
		toolsInfo.updateUI();
		JLabel label = new JLabel();
		int count = 0;
		ArrayList<Schedule> temp = new ArrayList<Schedule>();
		for(int i=0; i< neededTools.size(); i++){
			ArrayList<Schedule> s = neededTools.get(i).getSchedule();
			if(s.size()> temp.size()){
				temp = s;
				//System.out.println(s.get(s.size()-1).printDate());
			}
			for(int j =0; j<s.size(); j++){
				label = new JLabel();
				label.setFont(new Font("Tahoma", Font.PLAIN, 13));
				label.setAlignmentX(Component.CENTER_ALIGNMENT);
				String txt = "Componente '" + neededMaterials.get(i).getComponentName()+"' usara " +neededTools.get(i).getName()+" y producira: "+s.get(j).printDate();
				toolDates.add(txt);
				label.setText(txt);
				Object o = "cell 0 " + count + ",alignx left";
				toolsInfo.add(label, o);
				count++;
				//System.out.println(s.get(j).printDate());
			}
		}
		estimatedDelivery = temp.get(temp.size()-1).saveToFileFormat();
		
	}
	private void setProjectDisplays(){
		count = 0;
		for(int i=0; i< neededWorkers.size(); i++){
			if(neededWorkers.get(i).getSchedule().size() > 0){
				count++;
			}
		}
		lblneededWorkers.setText(count + " Personas");
		lblEstimatedTime.setText(estimatedDelivery);
		//lblEstimatedTime;
	}
	public void setProjectValues(String partNo, String deadline){
		lblPartNumber.setText(partNo);
		lblDeadline.setText(deadline);
	}
	private void readToolsSchedule(File f){
		String[] info;
		int count =0;
		int count2 =0;
		BufferedReader inputStream = null;
		try {
            inputStream = new BufferedReader(new FileReader(f));
            String l;
            toolsInfo.removeAll();
            workersInfo.removeAll();
            while ((l = inputStream.readLine()) != null) {
                info = l.split(",");                
                if(info[0].equals("tool")){                	
                	JLabel label = new JLabel();
    				label.setFont(new Font("Tahoma", Font.PLAIN, 13));
    				label.setAlignmentX(Component.CENTER_ALIGNMENT);
    				label.setText(info[1]);
    				Object o = "cell 0 " + count + ",alignx left";
    				toolsInfo.add(label, o);
    				count++;
                }
                else if(info[0].equals("worker")){
                	JLabel label = new JLabel();
    				label.setFont(new Font("Tahoma", Font.PLAIN, 13));
    				label.setAlignmentX(Component.CENTER_ALIGNMENT);
    				label.setText(info[1]);
    				Object o = "cell 0 " + count2 + ",alignx left";
    				workersInfo.add(label, o);
    				count2++;
                }
                else if(info[0].equals("Needed Workers")){
                	lblneededWorkers.setText(info[1] + " Personas");
                }
                else if(info[0].equals("Estimated Time")){
                	lblEstimatedTime.setText(info[1]);
                }
                else if(info[0].equals("PartNo")){
                	lblPartNumber.setText(info[1]);
                }
                else if(info[0].equals("Deadline")){
                	lblDeadline.setText(info[1]);
                }                	
            }
            toolsInfo.updateUI();
            workersInfo.updateUI();
		}
		catch(IOException e){	}
		finally {
            if (inputStream != null) {
                try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
		}
	}

}
